package edu.unf.cen6940.dao;

import edu.unf.cen6940.domain.Faculty;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Faculty entities.
 * 
 */
@Repository("FacultyDAO")
@Transactional
public class FacultyDAOImpl extends AbstractJpaDao<Faculty> implements
		FacultyDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Faculty.class }));

	/**
	 * EntityManager injected by Spring for persistence unit rms
	 *
	 */
	@PersistenceContext(unitName = "rms")
	private EntityManager entityManager;

	/**
	 * Instantiates a new FacultyDAOImpl
	 *
	 */
	public FacultyDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findFacultyByNnumberContaining
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByNnumberContaining(String nnumber) throws DataAccessException {

		return findFacultyByNnumberContaining(nnumber, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByNnumberContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByNnumberContaining(String nnumber, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByNnumberContaining", startResult, maxRows, nnumber);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByPrimaryKey
	 *
	 */
	@Transactional
	public Faculty findFacultyByPrimaryKey(Integer facultyId) throws DataAccessException {

		return findFacultyByPrimaryKey(facultyId, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByPrimaryKey
	 *
	 */

	@Transactional
	public Faculty findFacultyByPrimaryKey(Integer facultyId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFacultyByPrimaryKey", startResult, maxRows, facultyId);
			return (edu.unf.cen6940.domain.Faculty) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findFacultyByUrl
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByUrl(String url) throws DataAccessException {

		return findFacultyByUrl(url, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByUrl
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByUrl(String url, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByUrl", startResult, maxRows, url);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByActive
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByActive(String active) throws DataAccessException {

		return findFacultyByActive(active, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByActive
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByActive(String active, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByActive", startResult, maxRows, active);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByActiveContaining
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByActiveContaining(String active) throws DataAccessException {

		return findFacultyByActiveContaining(active, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByActiveContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByActiveContaining(String active, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByActiveContaining", startResult, maxRows, active);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByUrlContaining
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByUrlContaining(String url) throws DataAccessException {

		return findFacultyByUrlContaining(url, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByUrlContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByUrlContaining(String url, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByUrlContaining", startResult, maxRows, url);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByFirstName
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByFirstName(String firstName) throws DataAccessException {

		return findFacultyByFirstName(firstName, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByFirstName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByFirstName(String firstName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByFirstName", startResult, maxRows, firstName);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByNnumber
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByNnumber(String nnumber) throws DataAccessException {

		return findFacultyByNnumber(nnumber, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByNnumber
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByNnumber(String nnumber, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByNnumber", startResult, maxRows, nnumber);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByEmail
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByEmail(String email) throws DataAccessException {

		return findFacultyByEmail(email, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByEmail
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByEmail(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByEmail", startResult, maxRows, email);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByLastName
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByLastName(String lastName) throws DataAccessException {

		return findFacultyByLastName(lastName, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByLastName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByLastName(String lastName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByLastName", startResult, maxRows, lastName);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByFirstNameContaining
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByFirstNameContaining(String firstName) throws DataAccessException {

		return findFacultyByFirstNameContaining(firstName, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByFirstNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByFirstNameContaining(String firstName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByFirstNameContaining", startResult, maxRows, firstName);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByEmailContaining
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByEmailContaining(String email) throws DataAccessException {

		return findFacultyByEmailContaining(email, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByEmailContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByEmailContaining(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByEmailContaining", startResult, maxRows, email);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByFacultyId
	 *
	 */
	@Transactional
	public Faculty findFacultyByFacultyId(Integer facultyId) throws DataAccessException {

		return findFacultyByFacultyId(facultyId, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByFacultyId
	 *
	 */

	@Transactional
	public Faculty findFacultyByFacultyId(Integer facultyId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFacultyByFacultyId", startResult, maxRows, facultyId);
			return (edu.unf.cen6940.domain.Faculty) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllFacultys
	 *
	 */
	@Transactional
	public Set<Faculty> findAllFacultys() throws DataAccessException {

		return findAllFacultys(-1, -1);
	}

	/**
	 * JPQL Query - findAllFacultys
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findAllFacultys(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFacultys", startResult, maxRows);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * JPQL Query - findFacultyByLastNameContaining
	 *
	 */
	@Transactional
	public Set<Faculty> findFacultyByLastNameContaining(String lastName) throws DataAccessException {

		return findFacultyByLastNameContaining(lastName, -1, -1);
	}

	/**
	 * JPQL Query - findFacultyByLastNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Faculty> findFacultyByLastNameContaining(String lastName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFacultyByLastNameContaining", startResult, maxRows, lastName);
		return new LinkedHashSet<Faculty>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Faculty entity) {
		return true;
	}

	@Override
	public Set<Faculty> searchByTerm(String term) throws DataAccessException {
		Query query = createQuery("from Faculty myFaculty where myFaculty.firstName like ?1 or myFaculty.lastName like ?1", -1, -1, "%"+term+"%");
		return new LinkedHashSet<Faculty>(query.getResultList());
	}
}
